package org.example.filesfind;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//find by name
public class FileFindExample1 {
    public static void main(String[] args) {
        Path path = Paths.get("F:\\迅雷下载");
        List<Path> pathList = findByFileName(path,".json");
        System.err.println(JSONObject.toJSONString(pathList,true));
    }

    static List<Path> findByFileName(Path path, String fileName){
        List<Path> result = null;
        try (Stream<Path> pathStream = Files.find(path, Integer.MAX_VALUE, (p, att) -> p.getFileName().toString().endsWith(fileName))) {
            result = pathStream.collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
